import MapView from "@arcgis/core/views/MapView"; //视图
import FindTask from "@arcgis/core/tasks/FindTask";
import FindParameters from "@arcgis/core/tasks/support/FindParameters";
import { openLoading, closeLoading } from '../func/loading';
import { SymbolUtils } from "../../../utils/symbol-utils";
import { ElNotification } from "element-plus";
import Graphic from "@arcgis/core/Graphic";
export class FindTaskUtil {

    private _mapView: MapView;
    private _searchValue: string;
    private _layerURL: string;
    private _layerIds: number[];
    private _searchFields: string[];
    /**
     * 根据条件查询图层信息的类
     * @param mapView 地图的view对象
     * @param layerURL 服务地址
     * @param layerIds 查询图层ID集合
     * @param searchFields 查询字段集合
     * @param searchValue 查询的值
     */
    public constructor(mapView: MapView, layerURL: string, layerIds: number[], searchFields: string[], searchValue: string) {
        this._mapView = mapView;
        this._searchValue = searchValue;
        this._layerURL = layerURL;
        this._layerIds = layerIds;
        this._searchFields = searchFields;
    }

    /**
     * 根据条件查询图层信息
     * @returns 返回查询结果集合
     */
    public SearchResult<T>(): Promise<Array<T>> {
        openLoading();
        this.cleargraphics();
        const list = new Array<T>();
        // 创建属性查询的对象
        const findtask = new FindTask({
            url: this._layerURL
        })
        //通过Find方式查询 创建查询参数
        const params = new FindParameters({
            searchText: this._searchValue,
            // 查询的图层ID
            layerIds: this._layerIds,
            // 查询图层字段属性
            searchFields: this._searchFields,
            // 返回几何图形，决定高度亮度，结果显示
            returnGeometry: true
        })
        return new Promise((reslover, reject) => {
            findtask
                .execute(params)
                .then(response => {
                    const res = response.results;
                    if (res.length > 0) {
                        for (let index = 0; index < res.length; index++) {
                            let graphic;
                            //获得图形
                            graphic = res[index].feature;
                            graphic.symbol = SymbolUtils.getPointSymbol();
                            // 显示查询结果，添加到集合中，并返回                           
                            list.push(graphic.attributes);
                            //把feature添加到地图中，并且高亮
                            this._mapView.graphics.add(graphic);
                        }
                        closeLoading();
                        reslover(list);
                    } else {
                        closeLoading();
                        this.openNoticel("没有查询结果");
                        reslover(list);
                    }
                })
                .catch((ex) => {
                    closeLoading();
                    this.openNoticel("服务出现错误" + ex);
                })
        })
    }
    /**
     * 异步根据条件查询，并定位到指定未知展示点或面
     * @param symbolType 查询结果展示类型 点:point、面polyline
     */
    public SearchLocation<T>(symbolType: string): Promise<Array<T>> {
        openLoading();
        this.cleargraphics();
        const list = new Array<T>();
        // 创建属性查询的对象
        const findtask = new FindTask({
            url: this._layerURL
        })
        // 创建查询参数
        const params = new FindParameters({
            searchText: this._searchValue,
            // 查询的图层ID
            layerIds: this._layerIds,
            // 查询图层字段属性
            searchFields: this._searchFields,
            // 返回几何图形，决定高度亮度，结果显示
            returnGeometry: true
        })
        return new Promise((reslover, reject) => {
            findtask
                .execute(params)
                .then(response => {
                    const res = response.results;
                    if (res.length > 0) {
                        for (let index = 0; index < (res.length>10?10:res.length); index++) {                           
                            //获得图形                                                                                
                            let graphic = res[index].feature;
                            switch (symbolType) {
                                case 'point':
                                    graphic.symbol = SymbolUtils.getPointSymbol();
                                    break;
                                case 'polyline':
                                    graphic.symbol = SymbolUtils.getFillSymbol();
                                default:
                                    break;
                            }                            
                            list.push(graphic.attributes);
                            this._mapView.graphics.add(graphic);
                            reslover(list);
                            this._mapView.goTo(
                                {
                                    target: graphic,
                                    zoom: 10,
                                },
                                { duration: 2000 },
                            );
                        }
                        closeLoading();
                    } else {
                        closeLoading();
                        this.openNoticel("没有查询结果");
                    }
                })
                .catch((ex) => {
                    closeLoading();
                    console.log('查询服务异常', ex);
                    this.openNoticel("服务出现错误"+ex);
                })
        })
    }

    //警告提醒弹窗
    private openNoticel(warMeg: string) {
        ElNotification.error({
            title: "警告",
            message: warMeg,
            position: "bottom-right",
            offset: 200,
        });
    };
    // 清除地图上的graphics
    private cleargraphics() {
        this._mapView.graphics.removeAll();
    }

}
